﻿namespace BTS.COMMON
{
    using System;
    using System.Text;
    using System.Text.RegularExpressions;

    public abstract class StringHelper
    {
        private static string[][] shiftString = new string[][] { new string[] { "'", "$1" }, new string[] { "(", "$2" }, new string[] { ")", "$3" }, new string[] { "<", "$4" }, new string[] { ">", "$5" }, new string[] { "=", "$6" } };

        protected StringHelper()
        {
        }

        public static string ClearGarbageCode(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            str = ReplaceHtml("</?script[^>]*>", str, "");
            str = ReplaceHtml("(javascript|jscript|vbscript|vbs):", str, "");
            str = ReplaceHtml("on(mouse|exit|error|click|key)", str, "");
            str = ReplaceHtml(@"<\?xml[^>]*>", str, "");
            str = ReplaceHtml(@"<style>[\w\W]*?</style>", str, "");
            str = ReplaceHtml(@"<!--[\w\W]*?-->", str, "");
            return str;
        }

        public static string ClearHtml(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            str = ReplaceHtml("&#[^>]*;", str, "");
            str = ReplaceHtml("</?marquee[^>]*>", str, "");
            str = ReplaceHtml("</?object[^>]*>", str, "");
            str = ReplaceHtml("</?param[^>]*>", str, "");
            str = ReplaceHtml("</?embed[^>]*>", str, "");
            str = ReplaceHtml("</?table[^>]*>", str, "");
            str = ReplaceHtml("</?tr[^>]*>", str, "");
            str = ReplaceHtml("</?th[^>]*>", str, "");
            str = ReplaceHtml("</?p[^>]*>", str, "");
            str = ReplaceHtml("</?a[^>]*>", str, "");
            str = ReplaceHtml("</?img[^>]*>", str, "");
            str = ReplaceHtml("</?tbody[^>]*>", str, "");
            str = ReplaceHtml("</?li[^>]*>", str, "");
            str = ReplaceHtml("</?span[^>]*>", str, "");
            str = ReplaceHtml("</?div[^>]*>", str, "");
            str = ReplaceHtml("</?th[^>]*>", str, "");
            str = ReplaceHtml("</?td[^>]*>", str, "");
            str = ReplaceHtml("</?script[^>]*>", str, "");
            str = ReplaceHtml("(javascript|jscript|vbscript|vbs):", str, "");
            str = ReplaceHtml("on(mouse|exit|error|click|key)", str, "");
            str = ReplaceHtml(@"<\?xml[^>]*>", str, "");
            str = ReplaceHtml(@"<\/?[a-z]+:[^>]*>", str, "");
            str = ReplaceHtml("</?font[^>]*>", str, "");
            str = ReplaceHtml("</?b[^>]*>", str, "");
            str = ReplaceHtml("</?u[^>]*>", str, "");
            str = ReplaceHtml("</?i[^>]*>", str, "");
            str = ReplaceHtml("</?strong[^>]*>", str, "");
            str = ReplaceHtml(@"<style>[\w\W]*?</style>", str, "");
            str = ReplaceHtml(@"<!--[\w\W]*?-->", str, "");
            str = ReplaceHtml("&nbsp;", str, "");
            str = ReplaceHtml(" ", str, "");
            str = ReplaceHtml(@"\s", str, "");
            return str;
        }

        public static string SubString(string str, int len)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            if ((len == 0) || (StringLength(str) < len))
            {
                return str;
            }
            return str.Substring(0, len) + "...";
        }

        public static string CutString(string str, int len, string postfix)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            if ((len == 0) || (StringLength(str) < len))
            {
                return str;
            }
            byte[] bytes = new ASCIIEncoding().GetBytes(str);
            int index = 0;
            int num2 = 0;
            index = 0;
            while (index <= (bytes.Length - 1))
            {
                if (bytes[index] == 0x3f)
                {
                    num2 += 2;
                }
                else
                {
                    num2++;
                }
                if (num2 >= len)
                {
                    break;
                }
                index++;
            }
            if (string.IsNullOrEmpty(postfix))
            {
                return str.Substring(0, index + 1);
            }
            return (str.Substring(0, index + 1) + postfix);
        }

        public static string FillZeroToNum(int num, int digit)
        {
            string str = num.ToString();
            int length = str.Length;
            for (int i = 1; i <= (digit - length); i++)
            {
                str = "0" + str;
            }
            return str;
        }

        public static string FillZeroToNum1(int num, int digit)
        {
            string str = "";
            if (num.ToString().Length >= digit.ToString().Length)
            {
                for (int i = 0; i < digit; i++)
                {
                    str = str + num.ToString()[i];
                }
            }
            else
                str = num.ToString();
            return str;
        }

        public static string GetRandomName(string fileType)
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());
            string str = FillZeroToNum(random.Next(0x3e8), 3);
            if (string.IsNullOrEmpty(fileType))
            {
                return (DateTime.Now.ToString("yyyyMMddhhmmss") + str);
            }
            return (DateTime.Now.ToString("yyyyMMddhhmmss") + str + "." + fileType);
        }

        public static string GetRandomName1()
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());
            string str = FillZeroToNum1(random.Next(0x3e8), 2);
            return (DateTime.Now.ToString("yyyyMMddhhmmss") + str);
        }

        public static string InputEncode(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }
            for (int i = 0; i < shiftString.Length; i++)
            {
                input = input.Replace(shiftString[i][0], shiftString[i][1]);
            }
            return input;
        }

        public static string MakePassword(int pwdlen)
        {
            string str = "abcdefghijklmnpqrstuvwxyz123456789";
            string str2 = "";
            Random random = new Random(Guid.NewGuid().GetHashCode());
            for (int i = 0; i < pwdlen; i++)
            {
                int num = random.Next(str.Length);
                str2 = str2 + str[num];
            }
            return str2;
        }

        public static string OutputDecode(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }
            for (int i = 0; i < shiftString.Length; i++)
            {
                input = input.Replace(shiftString[i][1], shiftString[i][0]);
            }
            return input;
        }

        private static string ReplaceHtml(string pattern, string strHtml, string replacement)
        {
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            if (regex.IsMatch(strHtml))
            {
                return regex.Replace(strHtml, replacement);
            }
            return strHtml;
        }

        public static int StringLength(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return 0;
            }
            byte[] bytes = new ASCIIEncoding().GetBytes(str);
            int num = 0;
            for (int i = 0; i <= (bytes.Length - 1); i++)
            {
                if (bytes[i] == 0x3f)
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
            }
            return num;
        }

        public static string TextToHtml(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            str = str.Replace("\r\n", "<br />");
            str = str.Replace("\n", "<br />");
            str = str.Replace("\r", "<br />");
            return str;
        }

        public static string ToShortDate(string strDate)
        {
            if (string.IsNullOrEmpty(strDate))
            {
                return null;
            }
            return DateTime.Parse(strDate).ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 字符串正则验证
        /// </summary>
        /// <param name="str">要验证的字符串</param>
        /// <param name="type">验证的类型，0：是否为空，1：电话号，2：手机号，3：电话号或手机号，4：邮箱，5：是否数字</param>
        /// <returns>布尔值</returns>
        public static bool RegexMatch(string str, int type)
        {
            bool bl = true;
            string pattern = "";
            Regex regex = null;
            switch (type)
            {
                case (int)RegexType.IsNullOrEmpty:
                    bl = string.IsNullOrEmpty(str);
                    break;
                case (int)RegexType.Mobile:
                    pattern = @"^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$";
                    regex = new Regex(pattern);
                    bl = regex.IsMatch(str);
                    break;
                case (int)RegexType.Telephone:
                    pattern = @"^((\+86)|(86))?(13|14|15|18)\d{9}$";
                    regex = new Regex(pattern);
                    bl = regex.IsMatch(str);
                    break;
                case (int)RegexType.PhoneNumber:
                    pattern = @"^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))|(((\+86)|(86))?(13|14|15|18)\d{9})$";
                    regex = new Regex(pattern);
                    bl = regex.IsMatch(str);
                    break;
                case (int)RegexType.Email:
                    pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                    regex = new Regex(pattern);
                    bl = regex.IsMatch(str);
                    break;
                case (int)RegexType.IsNaN:
                    pattern = @"^\d+$";
                    regex = new Regex(pattern);
                    bl = regex.IsMatch(str);
                    break;
                default:
                    bl = false;
                    break;
            }
            return bl;
        }
    }
}

